<template >
   <el-dialog :title="title" :close-on-click-modal="false" :before-close="closeDialog" :visible.sync="visible" width="700px" append-to-body dialogDrag>
        <el-form ref="form" :inline="true" :model="form" class="form" :rules="rules" size="small" label-width="80px">
            <!-- <el-form-item   label="所属部门"  prop="deptId">   
                <treeselect v-model="form.deptId" :multiple="false" :options="treeData" :normalizer="normalizer"/>
            </el-form-item> -->
            <el-form-item label="用户名" prop="username"> 
                <el-input v-model="form.username" style="width:220px"></el-input>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-radio v-for="(value,key) in statusDicts" :key="key" v-model="form.status" :label="value">{{ key }}</el-radio>
            </el-form-item>
            <el-form-item label="昵称" prop="nickName"> 
                <el-input v-model="form.nickName" style="width:220px"></el-input>
            </el-form-item>  
            <el-form-item label="性别" prop="sex">
                <el-radio v-for="(value,key) in sexDicts" :key="key" v-model="form.sex" :label="value">{{ key }}</el-radio>
            </el-form-item>
            <el-form-item label="联系电话" prop="phone"> 
                <el-input v-model="form.phone" style="width:220px"></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="email"> 
                <el-input v-model="form.email" style="width:220px"></el-input>
            </el-form-item>
            <!-- <el-form-item   label="角色" prop="roless"> 
                <el-select v-model="form.roless" multiple filterable placeholder="请选择角色">
                    <el-option v-for="item in roles" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
            </el-form-item> -->
            <el-form-item label="备注">
                <el-input type="textarea" v-model="form.note" style="width: 534px;"></el-input>
            </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
            <el-button type="text"    @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="doSubmit" :loading="loading">确认</el-button>
        </div>
    </el-dialog>
</template>
<script>
// import { listDeptTree } from "@/api/sysDept";
// import { listRole } from "@/api/sysRole";
import { addAdmin,updateAdmin,selAdmin} from '@/api/user'
// import { Message } from "element-ui";
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
    inject:['reload'],
     components: {
        Treeselect
    },
    data() {
        return {
            rowId:'',
            title:'',
            roles: [],
            visible: false,
            treeData:[],
            sexDicts:this.$global.sex,
            statusDicts:this.$global.status,
            loading:false,  //确认提交按钮加载中
            form: {
                username:'',
                nickName:'',
                status: 1,
                sex: 1,
                note:'',
                phone:'',
                email:''
            },
            rules:{
                username: [{ required: true,message: '用户名不能为空'}],
                sex:      [{ required: true,message: '性别不能为空'}],
                status:   [{ required: true,message: '请选择状态'}],
                phone:    [{ required: true,message: '电话号码不能为空'}],
                nickName: [{ required: true,message: '昵称不能为空'}],
                roless:   [{ required: true,message: '角色不能为空'}],
            }
        };
    },
    methods: {
        closeDialog(){
            this.$refs['form'].resetFields();
            this.visible = false
            this.loading = false
            this.rowId = ''
            this.form = {username:'', nickName:'', status: 1, sex: 1, note:'', phone:'', email:''}
        },
        init(row,isAdd) {
            this.title = (isAdd ? '新增用户':'编辑用户')
            !!row && (this.rowId = row.id) && this.findUserById(this.rowId)
            this.$nextTick(() => {
                this.visible = true
            })
        },
        findUserById(id){
            this.apiSelAdminById(id)
        },
        doSubmit() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.loading = true
                    this.$confirm("确定?",'提示',{ type: 'warning'}).then( ()=>{
                        if(this.rowId){
                            this.apiUpdateAdmin(this.form)
                        }else{
                            this.apiAddAdmin(this.form)
                        }
                    }).catch(err=>{
                        this.loading =false
                    })
                }
            })
        },
        //==========>请求api接口<==========//
        apiSelAdminById(id){
            selAdmin(id).then(res=>{
                if(res.code === 200){
                    this.form ={...res.data}
                }
            },error=>{
                this.$message.error(res.message)
            })
        },
        apiUpdateAdmin(admin){
            updateAdmin(admin).then(res => {
                if(res.code === 200){
                    this.$message.success(res.message)
                    this.closeDialog()
                    this.reload()
                }
            },error =>{
                this.$message.error(res.message)
            })
        },
        apiAddAdmin(admin){
            addAdmin(admin).then(res => {
                if(res.code === 200){
                    this.$message.success(res.message)
                    this.closeDialog()
                    this.reload()
                }
            },error =>{
                this.$message.error(res.message)
            })
        }
        //==========>请求api接口<==========//
        // getData() {
        //     listDeptTree().then(res => {
        //         this.treeData = res.data;
        //     });
        //     listRole().then(res => {
        //         this.roles = res.data.list;
        //     });
        // },
        // normalizer(node) {
        //     return {
        //         label: node.name,
        //         children: node.children,
        //     }
        // },
    },
};
</script>
<style scoped>

</style>


